
compono.ui.Menu = compono.Class({
	name:"Menu",
	active:false,
	items:[],
	constructor:function(ul){
		this.base = this.$(ul);
		this.items = this.$T("a",this.base);
		this.items.forEach(function(a){
			this.$(a).$class = this;
			a.ul = this.$(a.next());
			if(a.parentNode.parentNode === this.base){
				a.event("click",function(e){
					this.$class.toggle(this,e);
					e.preventDefault();
					return false;
				});
				a.event("mouseover",function(e){
					var z=this.parentNode.parentNode.active;
					if(z&&z!==this&&this.ul){
						this.$class.close(z);
						this.$class.open(this,e);
					}
				});
			}else{
				a.event("mouseover",function(e){
					if(this.ul){
						if(this.parentNode.parentNode.active){
							this.$class.close(this.parentNode.parentNode.active,e);
						}
						this.$class.open(this,e);
					}
				});
			}
		},this);
		this.documentClose.scope = this;
	},
	toggle:function(a,e){
		if(a.ul){
			if(a.ul.hasClassName("open")){
				this.close(a,e);
			}else{
				this.open(a,e);
			}
			return false;
		}
	},
	open:function(a,e){
		this.$(a.parentNode).appendClassName("open");
		a.appendClassName("open");
		a.ul.appendClassName("open");
		this.$E().add("click",this.documentClose,document);
		this.$E().add("click",this.documentStopPropagation,this.base);
		this.active = a;
		a.parentNode.parentNode.active = a;
		return a;
	},
	close:function(a,e){
		this.$(a.parentNode).removeClassName("open");
		a.removeClassName("open");
		a.ul.removeClassName("open");
		if(a.ul.active){
			arguments.callee.call(this,a.ul.active,e);
		}
		this.$E().remove("click",this.documentClose,document);
		this.$E().remove("click",this.documentStopPropagation,this.base);
		return a;
	},
	documentClose:function(e){
		var _this = arguments.callee.scope;
		if(_this.active){
			var ul = _this.active.parentNode.parentNode;
			while(ul!==_this.base){
				_this.close(ul.previousElement,e);
				ul.active = false;
				ul = ul.parentNode.parentNode;
			}
			ul.active = false;
			_this.close(_this.active,e);
		}
	},
	documentStopPropagation:function(e){
		e.stopPropagation();
	}
})
